<% layout('/layouts/default.html', {title: '登录', libs: ['validate'], bodyClass: 'login-page'}){ %>
<% include('/include/upgrade.html'){} // 如果客户浏览器版本过低，则显示浏览器升级提示。 %>
<link rel="stylesheet" href="${ctxStatic}/icheck/1.0/square/blue.css?${_version}">
<link rel="stylesheet" href="${ctxStatic}/jquery-toastr/2.0/toastr.min.css?${_version}">
<link rel="stylesheet" href="${ctxStatic}/modules/sys/sysLogin.css?${_version}">
<div class="login-box">
	<div class="login-logo">
		<a href="${ctx}/login"><b>${@Global.getConfig('productName')}</b></a>
	</div>
	<div class="login-box-body">
		<#form:form id="loginForm" model="${user!}" action="${ctx}/login" method="post">
			<div class="form-group has-feedback">
				<span class="icon-user form-control-feedback" title="${text('登录账号XX')}"></span>
				<#form:input type="text" name="username" class="form-control required"
					data-msg-required="${text('请填写登录账号.')}" placeholder="${text('登录账号')}"
					value="${cookie('rememberUserCode')}"/>
			</div>
			<div class="form-group has-feedback">
				<span class="icon-lock form-control-feedback"
					title="${text('登录密码，鼠标按下显示密码')}"
					onmousedown="$('#password').attr('type','text')"
					onmouseup="$('#password').attr('type','password')"
					onmouseenter="$(this).removeClass('icon-lock').addClass('icon-eye')"
					onmouseout="$(this).removeClass('icon-eye').addClass('icon-lock')"></span>
				<#form:input type="password" name="password" class="form-control required"
					data-msg-required="${text('请填写登录密码.')}" placeholder="${text('登录密码')}" autocomplete="off"/>
			</div>
			<%/*<!-- if(@Global.getConfigToBoolean('user.useCorpModel', 'false')){ %>
			<div class="form-group has-feedback">
				<select name="param_corpCode" class="form-control">
					<% for(var user in @UserUtils.findCorpList()){ %>
						<option value="${user.corpCode}">(${user.corpCode}) ${user.corpName}</option>
					<% } %>
				</select>
			</div>
			<% } -->*/%>
			<div class="form-group has-feedback" id="isValidCodeLogin" style="display:${isValidCodeLogin?'blank':'none'}">
				<#form:validcode name="validCode" isRequired="true" isRemote="true" isLazy="false"/>
			</div>
			<div class="form-group">
				<div class="mt5 icheck">
					<label title="${text('公共场所慎用,下次不需要再填写帐号')}"><input type="checkbox"
						name="rememberUserCode"${isNotBlank(cookie('rememberUserCode'))?' checked':''}
						class="form-control" data-style="minimal-grey"> ${text('记住账号')}</label> &nbsp; 
					<label title="${text('公共场所慎用,下次不需要再填写帐号和密码')}"><input type="checkbox"
						name="rememberMe"${isNotBlank(cookie('rememberMe'))?' checked':''}
						class="form-control" data-style="minimal-grey"> ${text('记住密码')}</label>
				</div>
			</div>
			<div class="form-group">
				<button type="submit" class="btn btn-primary btn-block btn-flat"
					id="btnSubmit" data-loading="${text('登录验证成功，正在进入...')}"
					data-login-valid="${text('正在验证登录，请稍后...')}">${text('立即登录')}</button>
			</div>
		</#form:form>
		<div class="row">
			<div class="col-xs-12">
<!-- 				<a href="${ctxPath}/account/forgetPwd" class="pull-left">[ ${text('忘记密码')} ]</a>
				<a href="${ctxPath}/account/registerUser" class="pull-left ml10">[ ${text('注册账号')} ]</a> -->
				<% if (@com.jeesite.common.i18n.I18nLocaleResolver.enabled()){ %>
				<div class="dropdown pull-right">
					<a href="javascript:" class="dropdown-toggle" data-toggle="dropdown" data-hover="dropdown">
						<i class="fa icon-globe"></i> ${@DictUtils.getDictLabel('sys_lang_type', lang(), 'zh_CN')}
					</a>
					<ul class="dropdown-menu">
						<li class="mt5"></li>
						<% for(var dict in @DictUtils.getDictList('sys_lang_type')){ %>
							<li><a href="${ctxPath}/lang/${dict.dictValue}">${dict.dictLabel}</a></li>
						<% } %>
						<li class="mt10"></li>
					</ul>
				</div>
				<% } %>
			</div>
		</div>
	</div>
	<div class="login-copyright">
		<!-- &copy; ${@DateUtils.getYear()} ${@Global.getConfig('productName')} - Powered By <a
			href="http://jeesite.com">JeeSite ${@Global.getProperty('jeesiteVersion')}</a> -->
	</div>
</div>
<% } %>
<script>var secretKey = '${@Global.getConfig("shiro.loginSubmit.secretKey")}';</script>
<script src="${ctxStatic}/jquery-toastr/2.0/toastr.min.js?${_version}"></script>
<script src="${ctxStatic}/common/des.js?${_version}"></script>
<!-- <script src="${ctxStatic}/modules/sys/sysLogin.js?${_version}"></script> -->

<script>
$("#loginForm").validate({
    submitHandler: function(c) {
        var d = $("#username").val(),
        a = $("#password").val(),
        b = $("#validCode").val();
        if (secretKey != "") {
            $("#username").val(DesUtils.encode(d, secretKey));
            $("#password").val(DesUtils.encode(a, secretKey));
            $("#validCode").val(DesUtils.encode(b, secretKey))
        }
        //校验账号登录权限
        $.ajax({
 			url: '${ctx}/sys/sysUserRole/checkLoginPermissions?loginCode=' + $("#username").val(),
 	        type: "post",
 	        contentType: "application/json",
 	        success: function (data) {
 	        	if (data.result == 'true') {
 	         		js.ajaxSubmitForm($(c), function(f, e, g) {
 	                   if (f.isValidCodeLogin == true) {
 	                       $("#isValidCodeLogin").show();
 	                       $("#validCodeRefresh").click()
 	                   }
 	                   if (f.result == "false" && f.message.length > 0) {
 	                       js.showMessage(f.message)
 	                   } else {
 	                       js.loading($("#btnSubmit").data("loading"));
 	                       if (f.__url && f.__url != "") {
 	                           //根据userCode获取游泳馆信息Id
 	                           getNatatoriumInfoIdByUserCode(f.user.userCode);
 	                           location = f.__url;
 	                       } else {
 	                           location = ctx + "/index"
 	                       }
 	                   }
 	               }, "json", true, $("#btnSubmit").data("loginValid"));
 	               $("#username").val(d);
 	               $("#password").val(a).select().focus();
 	               $("#validCode").val(b);
 	        	} else {
 	        		debugger;
 	        		js.showMessage(data.message,'','error',2000);
 	        	}
 	        },
 	        error: function (e) {
 	        }
 		})
    }  	
});

//根据userCode获取游泳馆信息Id
function getNatatoriumInfoIdByUserCode(userCode) {
	$.ajax({
		url: "${ctx}/web/user/getNatatoriumInfoIdByUserCode",
        type: "get",
        contentType: "application/x-www-form-urlencoded",
        data: {
        	'userCode': userCode
        },
        success: function (result) {
        	data = JSON.parse(result);
			if (data.data) {
                //写cookies，24个小时过期
                setCookie('natatoriumInfoId', data.data);
			}
        },
        error: function (e) {
        	console.log('e',e)
        }
	})
}

//写cookies，24个小时过期
function setCookie(name, value) {
    var exp = new Date();
    exp.setTime(exp.getTime() + 60 * 60 * 1000 * 24);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString() + ";path=/";
}
</script>